package com.hyc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyc.enmus.ErrorEnum;
import com.hyc.entity.News;
import com.hyc.result.RespJson;
import com.hyc.service.NewsService;
import com.hyc.util.project.LayUIPage;
import com.hyc.util.project.PageCond;
import com.hyc.util.project.ProjectHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/news")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;

    @InitBinder
    protected void initBinder(ServletRequestDataBinder binder) throws Exception {
        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping("list")
    public LayUIPage listNews(News query) {
        PageCond page = getPageCond();
        if (query == null) {
            query = new News();
        }
        if (page == null) {
            page = ProjectHelper.buildPage();
        }
        PageHelper.startPage(page.getPageIndex(), page.getPageSize(), page.isShdCount());

        List<News> newsList = newsService.queryList(query);
        return layUIPage(new PageInfo<>(newsList));
    }

    @RequestMapping("find")
    public RespJson findById(Long id) {
        RespJson resp = new RespJson();
        try {
            News entity = newsService.findById(id);
            resp.setData(entity);
        } catch (Exception e) {
            log.error("查询文章异常", e);
            resp = new RespJson(ErrorEnum.ERROR);
        }
        return resp;
    }

    @RequestMapping("update")
    public RespJson update(News entity) {
        RespJson resp = new RespJson();
        try {
            newsService.update(entity);
            resp.setData(entity);
        } catch (Exception e) {
            log.error("更新文章异常", e);
            resp = new RespJson(ErrorEnum.ERROR);
        }
        return resp;
    }

    @RequestMapping("deleteBatch")
    public RespJson deleteBatch(@RequestParam(value = "ids[]") List<Long> ids) {
        RespJson resp = new RespJson();
        try {
            int count = newsService.deleteByIdList(ids);
            resp.setData(count);
        } catch (Exception e) {
            log.error("批量删除文章异常", e);
            resp = new RespJson(ErrorEnum.ERROR);
        }
        return resp;
    }

    @RequestMapping("newsAdd")
    public ModelAndView toAddPage(@RequestParam(value = "id", required = false) Long id, Model model) {
        model.addAttribute("id", id);
        return new ModelAndView("page/news/newsAdd");
    }

    @RequestMapping("addOrUpdate")
    public RespJson addOrUpdate(News entity){
        RespJson resp = new RespJson();
        try {
            newsService.addOrUpdate(entity);
            resp.setData(entity);
        } catch (Exception e) {
            log.error("维护文章异常", e);
            resp = new RespJson(ErrorEnum.ERROR);
        }
        return resp;
    }
}
